import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';

enum SnackBarContent {
  Waiting,
  Done,
  Error,
}

extension SnackBarContentExtension on SnackBarContent {
  String get name => describeEnum(this);

  Widget buildWidget({String? message, Widget? icon, Text? messageText}) {
    switch (this) {
      case SnackBarContent.Waiting:
        return WaitingRow(
            message: message,
            icon: icon,
            messageText: messageText,
            key: UniqueKey());
      case SnackBarContent.Done:
        return DoneRow(
            message: message,
            icon: icon,
            messageText: messageText,
            key: UniqueKey());
      case SnackBarContent.Error:
        return ErrowRow(
            message: message,
            icon: icon,
            messageText: messageText,
            key: UniqueKey());
    }
  }
}

class BaseContainer extends StatelessWidget {
  Widget child;

  BaseContainer({required this.child});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: Get.height / 12,
      child: Center(
        child: child,
      ),
    );
  }
}

class BaseRow extends StatelessWidget {
  Widget? icon;
  Text? message;

  BaseRow({Key? key, this.icon, this.message}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseContainer(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Flexible(
            flex: 1,
            child: Center(
              child: icon ??
                  Lottie.asset(
                    'assets/lottie_loading_plane.json',
                    fit: BoxFit.fill,
                  ),
            ),
          ),
          Flexible(
            flex: 3,
            child: Container(
              alignment: Alignment.centerLeft,
              child: message ??
                  Text(
                    '加载中……',
                    style: TextStyle(
                      fontSize: 18.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
            ),
          ),
        ],
      ),
    );
  }
}

class WaitingRow extends StatelessWidget {
  String? message;
  Widget? icon;
  Text? messageText;

  WaitingRow({Key? key, this.message, this.icon, this.messageText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseRow(
      icon: icon ??
          Lottie.asset(
            'assets/lottie_loading_plane.json',
            fit: BoxFit.fill,
          ),
      message: messageText ??
          Text(
            message ?? '加载中……',
            style: TextStyle(
              fontSize: 18.0,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
      key: key ?? UniqueKey(),
    );
  }
}

class DoneRow extends StatelessWidget {
  String? message;
  Widget? icon;
  Text? messageText;

  DoneRow({Key? key, this.message, this.icon, this.messageText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseRow(
      icon: icon ??
          Lottie.asset(
            'assets/lottie_add_success.json',
            fit: BoxFit.fill,
            width: Get.height / 13,
          ),
      message: messageText ??
          Text(
            message ?? '完成',
            style: TextStyle(
              fontSize: 18.0,
              fontWeight: FontWeight.bold,
              color: Colors.green,
            ),
          ),
      key: key ?? UniqueKey(),
    );
  }
}

class ErrowRow extends StatelessWidget {
  String? message;
  Widget? icon;
  Text? messageText;

  ErrowRow({Key? key, this.message, this.icon, this.messageText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseRow(
      icon: icon ??
          Lottie.asset(
            'assets/lottie_error.json',
            fit: BoxFit.contain,
            width: Get.height / 20,
            repeat: false,
          ),
      message: messageText ??
          Text(
            message ?? '发生错误',
            style: TextStyle(
              fontSize: 18.0,
              fontWeight: FontWeight.bold,
              color: Colors.red[400],
            ),
          ),
      key: key ?? UniqueKey(),
    );
  }
}
